/*
 * ConfigConnectionTab.java
 *
 * Created on 15 de Dezembro de 2008, 20:47
 */

package br.com.kibutx.bixochecker.gui;

import javax.swing.JFrame;

import br.com.kibutx.bixochecker.config.ConfigurationManager;
import br.com.kibutx.bixochecker.gui.config.Config;
import br.com.kibutx.bixochecker.gui.config.InvalidConfigException;

/**
 * 
 * @author moa
 */
public class ConfigConnectionTab extends javax.swing.JPanel implements Config {

	private static final long serialVersionUID = 1699411150220809451L;

	/** Creates new form ConfigConnectionTab */
	public ConfigConnectionTab() {
		initComponents();
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed" desc="Generated
	// Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		jpProxy = new javax.swing.JPanel();
		txtProxyHost = new javax.swing.JTextField();
		txtProxyPort = new javax.swing.JTextField();
		lblProxyHost = new javax.swing.JLabel();
		lblProxyPort = new javax.swing.JLabel();
		chkAuthEnabled = new javax.swing.JCheckBox();
		jpAuthentication = new javax.swing.JPanel();
		lblUsername = new javax.swing.JLabel();
		lblPassword = new javax.swing.JLabel();
		txtUsername = new javax.swing.JTextField();
		pwdPassword = new javax.swing.JPasswordField();
		chkProxyEnable = new javax.swing.JCheckBox();

		jpProxy
				.setBorder(javax.swing.BorderFactory
						.createTitledBorder(
								javax.swing.BorderFactory
										.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED),
								"Proxy"));

		txtProxyHost.setText("10.0.0.1");
		txtProxyHost.setEnabled(false);

		txtProxyPort.setText("3128");
		txtProxyPort.setEnabled(false);

		lblProxyHost.setText("Servidor proxy");

		lblProxyPort.setText("Porta");

		chkAuthEnabled.setText("Usar autenticação");
		chkAuthEnabled.setEnabled(false);
		chkAuthEnabled.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				chkAuthEnabledSelected(evt);
			}
		});

		jpAuthentication
				.setBorder(javax.swing.BorderFactory
						.createTitledBorder(
								javax.swing.BorderFactory
										.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED),
								"Autenticação"));

		lblUsername.setText("Usuário");

		lblPassword.setText("Senha");

		txtUsername.setEnabled(false);

		pwdPassword.setEnabled(false);

		org.jdesktop.layout.GroupLayout jpAuthenticationLayout = new org.jdesktop.layout.GroupLayout(
				jpAuthentication);
		jpAuthentication.setLayout(jpAuthenticationLayout);
		jpAuthenticationLayout
				.setHorizontalGroup(jpAuthenticationLayout
						.createParallelGroup(
								org.jdesktop.layout.GroupLayout.LEADING)
						.add(
								jpAuthenticationLayout
										.createSequentialGroup()
										.addContainerGap()
										.add(
												jpAuthenticationLayout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.LEADING)
														.add(
																jpAuthenticationLayout
																		.createSequentialGroup()
																		.add(
																				txtUsername,
																				org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																				111,
																				Short.MAX_VALUE)
																		.add(
																				12,
																				12,
																				12))
														.add(
																jpAuthenticationLayout
																		.createSequentialGroup()
																		.add(
																				lblUsername)
																		.addPreferredGap(
																				org.jdesktop.layout.LayoutStyle.RELATED)))
										.add(
												jpAuthenticationLayout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.LEADING)
														.add(
																pwdPassword,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																110,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
														.add(lblPassword))
										.addContainerGap()));
		jpAuthenticationLayout
				.setVerticalGroup(jpAuthenticationLayout
						.createParallelGroup(
								org.jdesktop.layout.GroupLayout.LEADING)
						.add(
								org.jdesktop.layout.GroupLayout.TRAILING,
								jpAuthenticationLayout
										.createSequentialGroup()
										.add(
												jpAuthenticationLayout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.BASELINE)
														.add(lblUsername).add(
																lblPassword))
										.addPreferredGap(
												org.jdesktop.layout.LayoutStyle.RELATED)
										.add(
												jpAuthenticationLayout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.BASELINE)
														.add(
																pwdPassword,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
														.add(
																txtUsername,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
										.addContainerGap()));

		org.jdesktop.layout.GroupLayout jpProxyLayout = new org.jdesktop.layout.GroupLayout(
				jpProxy);
		jpProxy.setLayout(jpProxyLayout);
		jpProxyLayout
				.setHorizontalGroup(jpProxyLayout
						.createParallelGroup(
								org.jdesktop.layout.GroupLayout.LEADING)
						.add(
								jpProxyLayout
										.createSequentialGroup()
										.addContainerGap()
										.add(
												jpProxyLayout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.LEADING)
														.add(
																jpProxyLayout
																		.createSequentialGroup()
																		.add(
																				jpProxyLayout
																						.createParallelGroup(
																								org.jdesktop.layout.GroupLayout.LEADING)
																						.add(
																								txtProxyHost,
																								org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																								154,
																								org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
																						.add(
																								lblProxyHost))
																		.add(
																				36,
																				36,
																				36)
																		.add(
																				jpProxyLayout
																						.createParallelGroup(
																								org.jdesktop.layout.GroupLayout.LEADING)
																						.add(
																								txtProxyPort,
																								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																								79,
																								Short.MAX_VALUE)
																						.add(
																								lblProxyPort,
																								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																								79,
																								Short.MAX_VALUE)))
														.add(
																org.jdesktop.layout.GroupLayout.TRAILING,
																jpAuthentication,
																org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																Short.MAX_VALUE)
														.add(chkAuthEnabled))
										.addContainerGap()));
		jpProxyLayout
				.setVerticalGroup(jpProxyLayout
						.createParallelGroup(
								org.jdesktop.layout.GroupLayout.LEADING)
						.add(
								jpProxyLayout
										.createSequentialGroup()
										.add(
												jpProxyLayout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.BASELINE)
														.add(lblProxyHost).add(
																lblProxyPort))
										.addPreferredGap(
												org.jdesktop.layout.LayoutStyle.RELATED)
										.add(
												jpProxyLayout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.BASELINE)
														.add(
																txtProxyHost,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
														.add(
																txtProxyPort,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												org.jdesktop.layout.LayoutStyle.UNRELATED)
										.add(chkAuthEnabled)
										.addPreferredGap(
												org.jdesktop.layout.LayoutStyle.RELATED,
												org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)
										.add(
												jpAuthentication,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
												89,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
										.add(6, 6, 6)));

		chkProxyEnable.setText("Usar proxy");
		chkProxyEnable.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				chkProxyEnabledSelected(evt);
			}
		});

		org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(
				this);
		this.setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(
				org.jdesktop.layout.GroupLayout.LEADING).add(
				org.jdesktop.layout.GroupLayout.TRAILING,
				layout.createSequentialGroup().addContainerGap().add(
						layout.createParallelGroup(
								org.jdesktop.layout.GroupLayout.TRAILING).add(
								org.jdesktop.layout.GroupLayout.LEADING,
								chkProxyEnable,
								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
								305, Short.MAX_VALUE).add(
								org.jdesktop.layout.GroupLayout.LEADING,
								jpProxy,
								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
								Short.MAX_VALUE)).addContainerGap()));
		layout.setVerticalGroup(layout.createParallelGroup(
				org.jdesktop.layout.GroupLayout.LEADING).add(
				layout.createSequentialGroup().addContainerGap().add(
						chkProxyEnable,
						org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 22,
						org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(
								org.jdesktop.layout.LayoutStyle.RELATED).add(
								jpProxy,
								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
								Short.MAX_VALUE).addContainerGap()));
	}// </editor-fold>//GEN-END:initComponents

	private void chkProxyEnabledSelected(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_chkProxyEnabledSelected
		boolean en = chkProxyEnable.isSelected();
		txtProxyHost.setEnabled(en);
		txtProxyPort.setEnabled(en);
		chkAuthEnabled.setEnabled(en);
		chkAuthEnabledSelected(evt);
	}// GEN-LAST:event_chkProxyEnabledSelected

	private void chkAuthEnabledSelected(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_chkAuthEnabledSelected
		boolean en = chkAuthEnabled.isEnabled();
		if (!chkAuthEnabled.isSelected()) {
			en = false;
		}
		txtUsername.setEnabled(en);
		pwdPassword.setEnabled(en);
	}// GEN-LAST:event_chkAuthEnabledSelected

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JCheckBox chkAuthEnabled;
	private javax.swing.JCheckBox chkProxyEnable;
	private javax.swing.JPanel jpAuthentication;
	private javax.swing.JPanel jpProxy;
	private javax.swing.JLabel lblPassword;
	private javax.swing.JLabel lblProxyHost;
	private javax.swing.JLabel lblProxyPort;
	private javax.swing.JLabel lblUsername;
	private javax.swing.JPasswordField pwdPassword;
	private javax.swing.JTextField txtProxyHost;
	private javax.swing.JTextField txtProxyPort;
	private javax.swing.JTextField txtUsername;

	// End of variables declaration//GEN-END:variables

	public static void main(String args[]) {
		JFrame win = new JFrame("sfdfsdf");
		win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		win.setContentPane(new ConfigConnectionTab());
		win.setVisible(true);
	}

	private void validateFields() throws InvalidConfigException {
		if (chkProxyEnable.isSelected()) {
			if(txtProxyHost.getText() == null || txtProxyHost.getText().equals("")){
				throw new InvalidConfigException(lblProxyHost.getText(),
						txtProxyHost.getText(), lblProxyHost.getText()
								+ " precisa ser especificado.");
			}
			Long value = null;
			try {
				value = new Long(txtProxyPort.getText());
			} catch (NumberFormatException nfe) {
				throw new InvalidConfigException(lblProxyPort.getText(),
						txtProxyPort.getText(), lblProxyPort.getText()
								+ " precisa ser um número.", nfe);
			}
			if (value.compareTo(new Long(0)) <= 0) {
				throw new InvalidConfigException(lblProxyPort.getText(),
						txtProxyPort.getText(), lblProxyPort.getText()
								+ " não pode ser um número negativo.");
			}
			if(chkAuthEnabled.isSelected()){
				if(txtUsername.getText() == null || txtUsername.getText().equals("")){
					throw new InvalidConfigException(lblUsername.getText(),
							txtUsername.getText(), lblUsername.getText()
									+ " precisa ser especificado.");
				}
				if(pwdPassword.getPassword() == null || pwdPassword.getPassword().length <= 0){
					throw new InvalidConfigException(lblPassword.getText(),
							"", lblPassword.getText()
									+ " precisa ser especificada.");
				}
			}
		}
	}

	public void load(ConfigurationManager config) {
		chkProxyEnable.setSelected(new Boolean(config.get(ConfigurationManager.CONN_PROXY_ENABLED)));
		txtProxyHost.setText(config.get(ConfigurationManager.CONN_PROXY_HOST));
		txtProxyPort.setText(config.get(ConfigurationManager.CONN_PROXY_PORT));
		chkAuthEnabled.setSelected(new Boolean(config.get(ConfigurationManager.CONN_PROXY_AUTH_ENABLED)));
		txtUsername.setText(config.get(ConfigurationManager.CONN_PROXY_AUTH_USERNAME));
		pwdPassword.setText(config.get(ConfigurationManager.CONN_PROXY_AUTH_PASSWORD));
		chkProxyEnabledSelected(null);
	}

	public void save(ConfigurationManager config) throws InvalidConfigException {
		// TODO Auto-generated method stub
		validateFields();
		config.set(ConfigurationManager.CONN_PROXY_ENABLED, ""+chkProxyEnable.isSelected());
		config.set(ConfigurationManager.CONN_PROXY_HOST, txtProxyHost.getText());
		config.set(ConfigurationManager.CONN_PROXY_PORT, txtProxyPort.getText());
		config.set(ConfigurationManager.CONN_PROXY_AUTH_ENABLED, ""+chkProxyEnable.isSelected());
		config.set(ConfigurationManager.CONN_PROXY_AUTH_USERNAME, txtUsername.getText());
		config.set(ConfigurationManager.CONN_PROXY_AUTH_PASSWORD, new String(pwdPassword.getPassword()));
	}
}
